class ClientsController < ApplicationController
  layout "application" 
  # GET /clients
  # GET /clients.xml
  def index
    @clients = Client.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @clients }
    end
  end

  # GET /clients/1
  # GET /clients/1.xml
  def show
    @client = Client.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @client }
    end
  end

  # GET /clients/new
  # GET /clients/new.xml
  def new
    @client = Client.new(params[:client])
    @client.created_by_user_id = session[:user].id
    @client.registration_date = Time.zone.now
    @client.county_id = @client.sel_county_id
    if request.post? and @client.save
      flash[:notice] = "Successfully created client..."
      @client = Client.new
    end
  end

  # GET /clients/1/edit
  def edit
    @client = Client.find(params[:id])    
  end

  # POST /clients
  # POST /clients.xml
  def create
    @client = Client.new(params[:client])
    respond_to do |format|
      if @client.save
        flash[:notice] = 'Client was successfully created.'
        format.html { redirect_to(@client) }
        format.xml  { render :xml => @client, :status => :created, :location => @client }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @client.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /clients/1
  # PUT /clients/1.xml
  def update
    @client = Client.find(params[:id])    
    @client1 = Client.new(params[:client])
    @county_id = @client1.sel_county_id
    if(@county_id == nil)
      @county_id = params[:county_id_hidden]
    end
    @client.county_id = @county_id
    respond_to do |format|
      if @client.update_attributes(params[:client])
        flash[:notice] = 'Client was successfully updated.'
        format.html { redirect_to(@client) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @client.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /clients/1
  # DELETE /clients/1.xml
  def delete
    @client = Client.find(params[:id])
    @client.destroy

    respond_to do |format|
      format.html { redirect_to(clients_url) }
      format.xml  { head :ok }
    end
  end
  
  def foundmodechanged()
    @foundmode = Foundmode.find(params[:id])
  end
  
  def search
    search = params[:search]  
    if(request.post? and search != nil)
      first_name = search[:first_name]
      last_name = search[:last_name]
      unique_id = search[:unique_id]
      
      @clients = Client.find :all, :conditions => ['unique_id like ? 
        and first_name like ? 
        and last_name like ?', 
        unique_id +"%",
        first_name + "%",
        last_name + "%"]    
      render :partial => 'search', :layout=>false
    end      
  end
  
  def communitychanged
    @county_id = Community.find(params[:id]).county.id
  end
end
